home *** CD-ROM | disk | FTP | other *** search
/ The CICA Windows Explosion! / The CICA Windows Explosion! - Disc 2.iso / programr / appsrcs.zip / APPPRINT.ZIP / APPPRINT.C next >
C/C++ Source or Header  |  1993-05-06  |  6KB  |  215 lines

  1. /*-------------------------------------------------------------------------
  2.  AppPrint.c
  3.  
  4.  A FilePrint tool for AppBar.
  5.  
  6.  by
  7.  GMP van kempen
  8.  NEVERnever Software 1993
  9.  
  10.  4.00.1  Initial version
  11.  4.00.2     Added select printer dialog (with thanks to Frits Wiarda)
  12.  4.00.4     Made "OK" button default push button.
  13.  
  14. ---------------------------------------------------------------------------*/
  15.  
  16. //compile with the strictest error checking
  17. #define STRICT
  18. #include <windows.h>
  19. #include <windowsx.h>
  20. #include <string.h>
  21. #include <stdio.h>
  22. #include <shellapi.h>
  23. #include <ctl3d.h>
  24. #include "appprint.h"
  25.  
  26. #define MAKECHILD(a,b,c,d,e,f,g,h) CreateWindow(a,b,WS_CHILD | WS_VISIBLE | c,d,e,f,g,hWnd,h,hInst,(LPSTR) NULL)
  27. #define MAKEBUTTON(a,b,c,d,e,f)    MAKECHILD("button",a,BS_PUSHBUTTON,b,c,d,e,f)
  28. #define MAKEDEFBUTTON(a,b,c,d,e,f) MAKECHILD("button",a,BS_DEFPUSHBUTTON,b,c,d,e,f)
  29. #define MAKETEXT(a,b,c,d,e,f)       MAKECHILD("static",a,SS_LEFT,b,c,d,e,f)
  30. #define MAKESTATIC(a,b,c,d)       MAKECHILD("static",NULL,SS_BLACKRECT,a,b,c,d,(HMENU) -1)
  31. #define MAKEBORDER(a,b,c,d)       MAKECHILD("static",NULL,SS_WHITERECT,a,b,c,d,(HMENU) -1)
  32.  
  33. char        szAppName[] = "AppPrint";
  34. HINSTANCE   hInst;
  35. BOOL        bPrintFile = FALSE;
  36. HWND hWndButton[3], hWndText[2];
  37.  
  38.  
  39. int PASCAL WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
  40.            LPSTR lpszCmdLine, int nCmdShow)
  41.     {
  42.     HWND    hWnd;
  43.     MSG     msg;
  44.     WNDCLASS    wndclass;
  45.     int     Error, xScreen, yScreen, yCaption, xSize, ySize;
  46.  
  47.     if(!hPrevInstance)
  48.     {
  49.     wndclass.style            = CS_HREDRAW | CS_VREDRAW;
  50.     wndclass.lpfnWndProc        = WndProc;
  51.     wndclass.cbClsExtra        = 0;
  52.     wndclass.cbWndExtra        = 0;
  53.     wndclass.hInstance        = hInstance;
  54.     wndclass.hIcon            = LoadIcon(hInstance, szAppName);
  55.     wndclass.hCursor        = LoadCursor(NULL, IDC_ARROW);
  56.     wndclass.hbrBackground        = GetStockBrush(LTGRAY_BRUSH);
  57.     wndclass.lpszMenuName        = NULL;
  58.     wndclass.lpszClassName        = szAppName;
  59.  
  60.     RegisterClass(&wndclass);
  61.     }
  62.  
  63.     hInst = hInstance;
  64.  
  65.     xScreen = GetSystemMetrics(SM_CXSCREEN);
  66.     yScreen = GetSystemMetrics(SM_CYSCREEN);
  67.     yCaption = GetSystemMetrics(SM_CYCAPTION);
  68.  
  69.     Ctl3dRegister(hInstance);
  70.     Ctl3dAutoSubclass(hInstance);
  71.  
  72.     lstrcpy(szBuffer, lpszCmdLine);
  73.     if(strlen(szBuffer))
  74.     {
  75.     bPrintFile = TRUE;
  76.     xSize = ySize = 0;
  77.     }
  78.     else
  79.     {
  80.     xSize = 260;
  81.     ySize = 133 + yCaption;
  82.     }
  83.  
  84.     hWnd = CreateWindow(szAppName, "AppPrint 4.0",
  85.             WS_POPUP | WS_SYSMENU | WS_CAPTION |
  86.             WS_MINIMIZEBOX | WS_VISIBLE,
  87.             (xScreen - xSize)/2, yScreen/2 - ySize,
  88.             xSize, ySize,
  89.             NULL, (HMENU) NULL, hInstance, NULL);
  90.  
  91.     if(bPrintFile)
  92.     {
  93.     bPrintFile = TRUE;
  94.     Error = (int) ShellExecute(hWnd, "print", lpszCmdLine, NULL, NULL, NULL);
  95.     if(Error == 31)
  96.        OkMsgBox("AppPrint 4.0","Could not print.\nNo association found\nfor dropped file.\n");
  97.     }
  98.     else
  99.     {
  100.     ShowWindow(hWnd, SW_SHOWNORMAL);
  101.     UpdateWindow(hWnd);
  102.     }
  103.  
  104.     while(GetMessage(&msg, NULL, 0, 0))
  105.     {
  106.     TranslateMessage(&msg);
  107.     DispatchMessage(&msg);
  108.     }
  109.  
  110.     Ctl3dUnregister(hInstance);
  111.  
  112.     return msg.wParam;
  113.     } /* end WinMain */
  114.  
  115. /*------------------------------------------------------------------------/
  116.    FUNCTION: WndProc()
  117. /------------------------------------------------------------------------*/
  118. long WINAPI WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
  119.     {
  120.     char *Button[3] = {"&Help","&OK","&Change"};
  121.     char Text[2][256], *szDevice, *szDriver, *szOutput;
  122.     char PrinterName[STRING_SIZE];
  123.     int i, Error, DroppedFiles;
  124.     HBRUSH hbrush;
  125.  
  126.     switch (message)
  127.     {
  128.     case WM_CREATE:
  129.         if(bPrintFile)
  130.         SendMessage(hWnd, WM_DESTROY, 0, 0L);
  131.         else
  132.         {
  133.         // create static frame around text fields
  134.         Ctl3dSubclassCtl(MAKEBORDER(0, 0, 258, 132));
  135.         Ctl3dSubclassCtl(MAKESTATIC(9, 9, 242, 82));
  136.         strcpy(Text[0], "A file print utility for AppBar");
  137.         GetProfileString("windows", "device", "<none>", szBuffer, 255);
  138.         szDevice = strtok(szBuffer, ",");
  139.         szDriver = strtok(NULL, ",");
  140.         szOutput = strtok(NULL, ",");
  141.         sprintf(Text[1], "Default printer:\n%s on %s",szDevice, szOutput);
  142.         hWndButton[0] = MAKEBUTTON(Button[0], 10, 100, 80, 24, IDC_BUTTONS);
  143.         hWndButton[1] = MAKEDEFBUTTON(Button[1], 10+80, 100, 80, 24, IDC_BUTTONS+1);
  144.         hWndButton[2] = MAKEBUTTON(Button[2], 10+160, 100, 80, 24, IDC_BUTTONS+2);
  145.         for(i=0;i<2;i++)
  146.             hWndText[i] = MAKETEXT(Text[i], 16, 16+i*22, 228, 20*(i+1), IDC_TEXT+i);
  147.         DragAcceptFiles(hWnd, TRUE);
  148.         }
  149.         return 0;
  150.  
  151.     case WM_SYSCOLORCHANGE:
  152.            Ctl3dColorChange();
  153.        break;
  154.  
  155.     case WM_CTLCOLOR:
  156.         hbrush = Ctl3dCtlColorEx(message, wParam, lParam);
  157.         if(hbrush != (HBRUSH) FALSE)
  158.         return hbrush;
  159.         else
  160.         return DefWindowProc(hWnd, message, wParam, lParam);
  161.  
  162.        case WM_DROPFILES:
  163.         DroppedFiles = DragQueryFile((HANDLE) wParam, 0xFFFF, (LPSTR) NULL, 0);
  164.         for(i=0;i<DroppedFiles;i++)
  165.             {
  166.             DragQueryFile((HANDLE) wParam, i, szBuffer, sizeof(szBuffer));
  167.             Error = ShellExecute(hWnd, "print", szBuffer, NULL, NULL, NULL);
  168.             if(Error == 31)
  169.             OkMsgBox("AppPrint 4.0","Could not print.\nNo association found\nfor dropped file.\n");
  170.             }
  171.         DragFinish((HANDLE) wParam);
  172.         return 0;
  173.  
  174.        case WM_KEYDOWN:
  175.         if(wParam == VK_RETURN)
  176.         SendMessage(hWnd, WM_COMMAND, IDC_BUTTONS+1, 0);
  177.         return 0;
  178.  
  179.        case WM_COMMAND:
  180.         switch(wParam)
  181.         {
  182.         case IDC_BUTTONS: /* help */
  183.             WinHelp(hWnd, "AppTools.hlp",HELP_CONTENTS, 0L);
  184.             break;
  185.  
  186.         case IDC_BUTTONS+1:  /* Ok */
  187.             SendMessage(hWnd, WM_DESTROY, 0, 0);
  188.             break;
  189.  
  190.         case IDC_BUTTONS+2: /* Change */
  191.             if(SelectPrinterDialog(hInst, hWnd, PrinterName) == 0)
  192.             {
  193.             GetProfileString("devices", PrinterName, "<none>", szBuffer, 255);
  194.             strcat(PrinterName,",");
  195.             strcat(PrinterName,szBuffer);
  196.             WriteProfileString("windows","device", PrinterName);
  197.             szDevice = strtok(PrinterName, ",");
  198.             szDriver = strtok(NULL, ",");
  199.             szOutput = strtok(NULL, ",");
  200.             sprintf(szBuffer, "Default printer:\n%s on %s",szDevice, szOutput);
  201.             Static_SetText(hWndText[1], (LPCSTR) szBuffer);
  202.             }
  203.             break;
  204.         }
  205.         break;
  206.  
  207.        case WM_DESTROY:    // kill the whole program
  208.         if(bPrintFile)
  209.         DragAcceptFiles(hWnd, FALSE);
  210.         PostQuitMessage(0);
  211.         return 0;
  212.     }
  213.     return DefWindowProc(hWnd, message, wParam, lParam);
  214.     } /* end WndProc */
  215.